﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace BMproductions.Media.MediaPlayer.WMPPlus {
	public class WMPMenu: WMPPlusModule {
		internal IntPtr Native {
			get;
			private set;
		}

		internal IDictionary<IntPtr, WMPMenuItem> Items { get; private set; }
		internal IDictionary<IntPtr, WMPSubMenu> Menus { get; private set; }

		public WMPMenu(WMPPlusPublic owner, IntPtr native)
			: base(owner) {
			Native = native;
			Items = new Dictionary<IntPtr, WMPMenuItem>();
			Menus = new Dictionary<IntPtr, WMPSubMenu>();
		}

		protected override void Init() { }
		public override void Dispose() {
			foreach (WMPMenuItem item in Items.Values)
				item.Free();
			foreach (WMPSubMenu menu in Menus.Values)
				menu.Free();
		}

		public int PlaceSubMenu(IntPtr menu, IntPtr hMenu) {
			WMPSubMenu submenu;
			if (!Menus.TryGetValue(menu, out submenu))
				return -1;
			return submenu.Place(hMenu);
		}

		public void InvokeSubMenu(IntPtr menu, IntPtr hMenu) {
			WMPSubMenu submenu;
			if (Menus.TryGetValue(menu, out submenu))
				submenu.Invoke(hMenu);
		}

		public void InvokeMenuItem(IntPtr item, IntPtr hWnd) {
			WMPMenuItem mi;
			if (Items.TryGetValue(item, out mi))
				mi.Invoke(hWnd);
		}
	}
}
